﻿using System;
using System.Web.UI.WebControls;
using Alserve.Ffl.Data;
using Alserve.Ffl;

public partial class ffl_user_mysquad : System.Web.UI.Page
{
    private Int32 _showUserId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Id"] != null)
            {
                Skip thisSkip = new Skip();
                thisSkip.Where.Keyword.Value = "Status";
                thisSkip.Where.SkipNumber.Value = 1;
                Boolean inRunning = thisSkip.Query.Load();
                if (inRunning || Security.IsAdmin(Page))
                {
                    _showUserId = Convert.ToInt32(Request.QueryString["Id"]);
                    PopulatePositionDropDown();
                }
            }
            else
            {
                _showUserId = Convert.ToInt32(Session["UserId"]);
                PopulatePositionDropDown();
            }
            
        }
    }

    private void PopulatePositionDropDown()
    {
        Position thisPosition = new Position();
        thisPosition.LoadAll();
        PositionRepeater.DataSource = thisPosition.DefaultView;
        PositionRepeater.DataBind();
    }


    protected void PositionRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            Alserve.Ffl.Data.User thisUser = new Alserve.Ffl.Data.User();
            thisUser.Where.Id.Value = _showUserId;
            if (thisUser.Query.Load())
            {
                Label teamNameLabel = (Label)e.Item.FindControl("TeamNameLabel");
                teamNameLabel.Text = thisUser.TeamName;
            }
        }

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater playerPositionRepeater = (Repeater)e.Item.FindControl("PlayerPositionRepeater");
            HiddenField positionHidden = (HiddenField)e.Item.FindControl("PositionIdHidden");
            Int32 positionId = Convert.ToInt32(positionHidden.Value);

            UserPlayerScores thisUps = new UserPlayerScores();
            thisUps.Where.UserId.Value = _showUserId;
            thisUps.Where.PositionId.Value = positionId;

            thisUps.Query.AddResultColumn(UserPlayerScores.ColumnNames.PlayerId);
            if (thisUps.Query.Load())
            {
                playerPositionRepeater.DataSource = thisUps.DefaultView;
                playerPositionRepeater.DataBind();
            }
        }
    }
}
